package com.yuluke.ylsaudiorecorder.record;

import android.app.Service;
import android.content.Intent;
import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yuluke.ylsaudiorecorder.FileUtils;

import java.io.File;

public class RecordService extends Service {

    private static final String TAG = "YlsRec_Service";
    private static final String ACTION_START_RECORD = "com.yuluke.ylsaudiorecorder.action.START_RECORD";
    private static final String ACTION_STOP_RECORD = "com.yuluke.ylsaudiorecorder.action.STOP_RECORD";
    private WorkerHandler mWorkerHander;
    private HandlerThread mWorkerThread;

    public static void startRecord(Context context) {
        Log.i(TAG, "startRecord");
        Intent intent = new Intent(context, RecordService.class);
        intent.setAction(ACTION_START_RECORD);
        context.startService(intent);
    }

    public static void stopRecord(Context context) {
        Log.i(TAG, "stopRecord");
        Intent intent = new Intent(context, RecordService.class);
        intent.setAction(ACTION_STOP_RECORD);
        context.startService(intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mWorkerThread = new HandlerThread("YlsRec_Service");
        mWorkerThread.start();
        mWorkerHander = new WorkerHandler(mWorkerThread.getLooper(), FileUtils.getPcmFileDir(this));
    }

    public RecordService() {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand");
        if (intent != null) {
            final String action = intent.getAction();
            Log.i(TAG, "onStartCommand, action:" + action);
            if (ACTION_START_RECORD.equals(action)) {
                mWorkerHander.sendEmptyMessage(MSG_START);
            } else if (ACTION_STOP_RECORD.equals(action)) {
//                mWorkerHander.sendEmptyMessage(MSG_STOP);
                StateInstance.getInstance().change(StateInstance.STATE_RECORD_STOPED);
                mWorkerThread.quitSafely();
            }
        }
        return Service.START_NOT_STICKY;
    }

    private static final int MSG_START = 1;
    private static final int MSG_STOP = 2;

    private static class WorkerHandler extends Handler {

        private final RecordTask mRecTask;

        public WorkerHandler(@NonNull Looper looper, String path) {
            super(looper);
            mRecTask = new RecordTask(path);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            Log.i(TAG, "WorkerHandler handleMessage:" + msg.what);
            switch (msg.what) {
                case MSG_START:
                    handleActionStartRecord();
                    break;
                case MSG_STOP:
                    handleActionStopRecord();
                    break;
            }
        }

        // 需要在worker thread
        private void handleActionStartRecord() {
            Log.i(TAG, "handleActionStartRecord");
            mRecTask.start();
        }

        // 需要在worker thread
        private void handleActionStopRecord() {
            Log.i(TAG, "handleActionStopRecord");
            mRecTask.stop();
        }
    }
}